package com.aidex.biz.controller;

import com.aidex.biz.domain.News;
import com.aidex.biz.service.NewsService;
import com.aidex.biz.uitls.TargetLinkUitls;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 新闻管理Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/biz/bizNews")
public class NewsController extends BaseController {
    @Autowired
    private NewsService newsService;

    /**
     * 查询新闻管理列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizNews:list')")
    @GetMapping("/list")
    public R<PageInfo> list(News news, HttpServletRequest request, HttpServletResponse response) {
//        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
//                news.setCompanyId(null);
//        }else{
//                     news.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
//                 }
        news.setPage(new PageDomain(request, response));
        return R.data(newsService.findPage(news));
    }

    /**
     * 获取新闻管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizNews:query')")
    @GetMapping(value = "/{id}")
    public R<News> detail(@PathVariable("id") String id) {
        return R.data(newsService.get(id));
    }

    /**
     * 新增新闻管理
     */
    @PreAuthorize("@ss.hasPermi('biz:bizNews:add')")
    @Log(title = "新闻管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated News news) {
        return R.status(newsService.save(news));
    }

    /**
     * 修改新闻管理
     */
    @PreAuthorize("@ss.hasPermi('biz:bizNews:edit')")
    @Log(title = "新闻管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated News news) {
        return R.status(newsService.save(news));
    }


    /**
     * 删除新闻管理
     */
    @PreAuthorize("@ss.hasPermi('biz:bizNews:remove')")
    @Log(title = "新闻管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(newsService.deleteNewsByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizNews:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(newsService.findMaxSort(new News()));
    }

    /**
     * 导出新闻管理列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizNews:export')")
    @Log(title = "新闻管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(News news) {

//            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
//                    news.setCompanyId(null);
//            }else{
//                         news.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
//                     }
        List<News> list = newsService.findList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        return util.exportExcel(list, "新闻管理数据");
    }


    @GetMapping(value = "/targetLink")
    public R targetLink() {
        return R.data(TargetLinkUitls.getTargetLink(SecurityUtils.getLoginUser().getUser().getPhonenumber()));
    }

}
